import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/modules/cart/views/cart_item_view_view.dart';
import 'package:xiaomi_shop/app/services/screenAdapter.dart';
import 'package:xiaomi_shop/models/index.dart';

import '../controllers/cart_controller.dart';

class CartView extends GetView<CartController> {
  const CartView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade100,
      appBar: AppBar(
        title: const Text('购物车'),
        centerTitle: true,
        scrolledUnderElevation: 0,
        actions: [
          TextButton(
              onPressed: () {
                controller.isEdit.value = !controller.isEdit.value;
              },
              child: Obx(() => Text(
                    controller.isEdit.value ? "完成" : "编辑",
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: ScreenAdapter.fontSize(40)),
                  ))),
        ],
      ),
      body: Stack(
        children: [
          _content(),
          _bottom(),
        ],
      ),
    );
  }

  _content() {
    return Positioned(
        left: 0,
        right: 0,
        top: 0,
        bottom: ScreenAdapter.height(200),
        child: Obx(() => controller.list.value.isEmpty
            ? const Center(
                child: Text("购物车为空"),
              )
            : ListView.builder(
                padding: EdgeInsets.all(ScreenAdapter.width(30)),
                itemCount: controller.list.value.length,
                itemBuilder: (context, index) {
                  ShopModel model = controller.list.value[index];
                  return InkWell(onTap: () {}, child: CartItemViewView(model));
                },
              )));
  }

  _bottom() {
    return Positioned(
        left: 0,
        right: 0,
        bottom: 0,
        height: ScreenAdapter.height(200),
        child: Container(
            padding: EdgeInsets.fromLTRB(
                ScreenAdapter.width(20), 0, ScreenAdapter.width(20), 0),
            alignment: Alignment.topCenter,
            decoration: BoxDecoration(
                border: Border(top: BorderSide(color: Colors.grey.shade300))),
            child: Obx(
              () => Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Checkbox(
                          value: controller.selectAll.value,
                          activeColor: Colors.red,
                          shape: CircleBorder(
                              side: BorderSide(color: Colors.grey.shade100)),
                          onChanged: (selected) {
                            controller.selectAll.value = selected!;
                            controller.selectAllShop();
                          }),
                      Text(
                        controller.selectAll.value ? "取消" : "全选",
                        style: TextStyle(
                            fontSize: ScreenAdapter.fontSize(40),
                            fontWeight: FontWeight.bold),
                      )
                    ],
                  ),
                  controller.isEdit.value
                      ? OutlinedButton(
                          style: ButtonStyle(
                              backgroundColor:
                                  MaterialStatePropertyAll(Colors.white),
                              foregroundColor:
                                  MaterialStatePropertyAll(Colors.grey),
                              elevation: MaterialStatePropertyAll(0),
                              side: MaterialStatePropertyAll(
                                  BorderSide(color: Colors.grey)),
                              textStyle: MaterialStatePropertyAll(TextStyle(
                                fontSize: ScreenAdapter.fontSize(40),
                              ))),
                          onPressed: controller.deleteShop,
                          child: Text(
                            "删除",
                          ))
                      : Row(
                          children: [
                            Text("结算:",
                                style: TextStyle(
                                    fontSize: ScreenAdapter.fontSize(40),
                                    fontWeight: FontWeight.normal)),
                            Text(
                              "¥${controller.totalPrice}",
                              style: TextStyle(
                                  fontSize: ScreenAdapter.fontSize(50),
                                  color: Colors.orange.shade900,
                                  fontWeight: FontWeight.bold),
                            ),
                            SizedBox(
                              width: ScreenAdapter.width(30),
                            ),
                            ElevatedButton(
                                style: ButtonStyle(
                                    backgroundColor:
                                        MaterialStatePropertyAll(Colors.orange),
                                    foregroundColor:
                                        MaterialStatePropertyAll(Colors.white),
                                    elevation: MaterialStatePropertyAll(0),
                                    textStyle: MaterialStatePropertyAll(
                                        TextStyle(
                                            fontSize:
                                                ScreenAdapter.fontSize(40),
                                            fontWeight: FontWeight.bold))),
                                onPressed: () {
                                  // 结算选择的商品
                                  controller.settlement();
                                },
                                child: Text(
                                  "结算",
                                ))
                          ],
                        )
                ],
              ),
            )));
  }
}
